<?
$sort = getValue('sort');
$view = getValue('view', 'str', 'GET', 'list'); // list, grid

$sqlWhere = '';
if ($current_cat)
{
	$sqlWhere .= ' AND cat_id IN (' . $current_cat['cat_all_child'] . ') ';
}
else
{
	if ($iCat > 0)
	{
		$sqlWhere .= ' AND cat_id=' . $iCat . ' ';
	}
}
if ($keyword != '')
{
	$sqlWhere	.= " AND (pro_name LIKE '%" . $keyword . "%' OR pro_teaser LIKE '%" . $keyword . "%' OR pro_description LIKE '%" . $keyword . "%') ";
}

switch ($sort)
{
    case 1:
        $sqlOrderBy = 'pro_price ASC';
        break;
    case 2:
        $sqlOrderBy = 'pro_price DESC';
        break;
	case 3:
        $sqlOrderBy = 'pro_name DESC';
        break;
	case 4:
        $sqlOrderBy = 'pro_name DESC';
        break;
	case 5:
        $sqlOrderBy = 'pro_date DESC';
        break;
    default:
        $sqlOrderBy = 'pro_date DESC';
        break;
}
//Get page break params
$page_size		= 18;
$page_prefix	= "Trang: ";
$normal_class	= "page";
$selected_class= "page_current";
$previous		= "<";
$next				= ">";
$first			= "<<";
$last				= ">>";
$break_type		= 1;//"1 => << < 1 2 [3] 4 5 > >>", "2 => < 1 2 [3] 4 5 >", "3 => 1 2 [3] 4 5", "4 => < >"
$url				= getURL(0,1,1,1,"page");
$db_count		= new db_query("SELECT COUNT(*) AS count
										 FROM (categories_multi, products_multi, brands) INNER JOIN product_hits ON pro_id = ph_id
									 	 WHERE cat_id = pro_category_id AND bra_id = pro_brand_id AND categories_multi.lang_id = " . $lang_id . $sqlWhere);
$listing_count	= mysql_fetch_array($db_count->result);
$total_record	= $listing_count["count"];
$current_page	= getValue("page", "int", "GET", 1);
if($total_record % $page_size == 0) $num_of_page = $total_record / $page_size;
else $num_of_page = (int)($total_record / $page_size) + 1;
if($current_page > $num_of_page) $current_page = $num_of_page;
if($current_page < 1) $current_page = 1;
$db_count->close();
unset($db_count);
//End get page break params
$db_listing	= new db_query("SELECT *
									 FROM (categories_multi, products_multi, brands) INNER JOIN product_hits ON pro_id = ph_id
									 WHERE cat_id = pro_category_id AND bra_id = pro_brand_id AND categories_multi.lang_id = " . $lang_id . $sqlWhere . "
									 ORDER BY " . $sqlOrderBy . "
									 LIMIT " . ($current_page - 1) * $page_size . ", " . $page_size);

?>
<div class="navigate">
	<div class="L"></div>
    <div class="C">
    	<a class="home" href="<?=$con_root_path?>" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Trang_chu')?></a>
        <img src="<?=$theme_imagepath?>navigate_icon.gif" /><span><?=tdt('Thong_tin_tim_kiem_ve')?> <?=$keyword?></span>
	</div>
    <div class="R"></div>
</div>
<div class="module_top"><div class="fl"></div><div class="fr"></div></div>
<div class="padding module_larger_border">
<? if ($total_record == 0){?>
	<div class="data_is_updating" lang="<?=$array_lang[$lang_id][0];?>"><?=tdt('San_pham_dang_duoc_cap_nhat');?>!</div>
<? } else {?>
	<div class="product_option_select">
    	<div class="fl">
        	<b gTrans="<?=$array_lang[$lang_id][0]?>"><?=tdt('Co')?> <span class="count"><?=$total_record?></span> <?=tdt('san_pham')?></b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            <b><span lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Hien_thi')?></span> :</b>&nbsp; <? if ($show == 0) {?><span class="current" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Danh_sach')?></span><? } else {?><a href="<?=getURL(0,1,1,1,'show')?>&show=0" lang="<?=$array_lang[$lang_id][0]?>" onclick="save_cookie('showcolum', 0)"><?=tdt('Danh_sach')?></a><? }?> | <? if ($show == 1) {?><span class="current" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Grid')?></span><? } else {?><a href="<?=getURL(0,1,1,1,'show')?>&show=1" lang="<?=$array_lang[$lang_id][0]?>" onclick="save_cookie('showcolum', 1)"><?=tdt('Grid')?></a><? }?>
        </div>
        <div class="fr">
        	<?
			$arrSort = array(3 => tdt('Gia_tang_dan'),
							 4 => tdt('Gia_giam_dan'),
							 8 => tdt('Ua_thich_nhat'),
							 6 => tdt('Moi_nhat'),
							 );
			?>
        	<select class="form_control" onchange="window.location.href='<?=getURL(0,1,1,1,'sort')?>&sort=' + this.value">
            	<option value="0" lang="<?=$array_lang[$lang_id][0]?>">--[<?=tdt('Sap_xep_san_pham')?>]--</option>
                <? foreach ($arrSort as $key => $value) {?>
                <option value="<?=$key?>" lang="<?=$array_lang[$lang_id][0]?>"<?=($key==$sort)?' selected="selected"':''?>><?=$value?></option>
                <? }?>
			</select>
		</div>
        <div class="clear"></div>
	</div>
    <form class="form" name="listudv_form" action="<?=$con_root_path?>compare.php?iCat=<?=$iCat?>" method="post" target="_blank">
    <? if ($show == 0) {?>
        <table class="product_table_list" cellpadding="0" cellspacing="0">
            <tr class="title">
                <td width="5%" colspan="2"><!--<center><a class="compare" href="#compare" onclick="document.listudv_form.submit()"></a></center>--></td>
                <td width="80%">
                	<span lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Ten_san_pham')?></span><br />
                    <?=generate_sort("asc", 1, $sort, $theme_imagepath)?>
					<?=generate_sort("desc", 2, $sort, $theme_imagepath)?>
				</td>
                <td>
                	<span lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Gia_ban')?></span><br />
                    <?=generate_sort("asc", 3, $sort, $theme_imagepath)?>
					<?=generate_sort("desc", 4, $sort, $theme_imagepath)?>
				</td>
            </tr>
            <?
			$No = 0;
            while ($listing = mysql_fetch_array($db_listing->result))
			{
				++$No;
				$detail_url = generate_detail_url('product', $listing['cat_id'], $listing['pro_id'], $listing['pro_name']);
			?>
            <tr class="list">
                <td class="No"><?=$No?><br /><input name="compare_check[]" type="checkbox" value="<?=$listing['pro_id']?>" /></td>
                <?
                //Get new dimensions
				if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/products_pictures/larger_' . $listing['pro_picture']))
				{
					list($width, $height) = getimagesize($_SERVER['DOCUMENT_ROOT'] . '/products_pictures/larger_' . $listing['pro_picture']);
				}
				else
				{
					$width = 0;
					$height = 0;
				}
				?>
                <td align="center">
                	<div class="product_picture"><center><span><a class="product_picture_thumbnail" title="<?=$listing['pro_name']?>" href="<?=$detail_url?>" rel="<img src='/products_pictures/<?=$listing['pro_picture']?>' width='<?=$width?>' height='<?=$height?>' />" style="background-image:url(/products_pictures/small_<?=$listing['pro_picture']?>)"></a></span></center></div>
				</td>
                <td valign="top">
                	<div class="product_name"><a href="<?=$detail_url?>"><?=$listing['pro_name']?></a></div>
                    <div id="id_text_<?=$listing['pro_id']?>" class="product_teaser product_teaser_close"><?=$listing['pro_teaser']?></div>
                    <? if (strlen($listing['pro_teaser']) > 200) {?>
                    <div class="product_teaser_bnt">
                    	<div id="id_nut_open_<?=$listing['pro_id']?>" class="open"><a href="javascript:open_teaser('id_text_<?=$listing['pro_id']?>', 'id_nut_open_<?=$listing['pro_id']?>', 'id_nut_close_<?=$listing['pro_id']?>')" rel="nofollow" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Mo_rong')?></a></div>
                    	<div id="id_nut_close_<?=$listing['pro_id']?>" class="close" style="display:none"><a href="javascript:close_teaser('id_text_<?=$listing['pro_id']?>', 'id_nut_open_<?=$listing['pro_id']?>', 'id_nut_close_<?=$listing['pro_id']?>')" rel="nofollow" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Thu_nho')?></a></div>
					</div>
                    <? }?>
				</td>
                <td>
                    <div class="product_price"><div class="price"><?=($listing['pro_price'] > 0)?format_number($listing['pro_price']) . ' VNĐ':$con_message_zero_price?></div></div>
                    <div class="product_quantity"><font lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Trong_kho')?></font>: <span class="count"><?=($listing['pro_quantity']==0)?tdt('Het_hang'):$listing['pro_quantity']?></span></div>
                    <? if ($listing['pro_price'] > 0 && $listing['pro_quantity'] > 0) {?>
                    <div class="add_to_cart"><a title="<?=tdt('Mua_hang')?>" href="<?=$con_root_path?>addtocart.php?iPro=<?=$listing['pro_id']?>&return=<?=base64_encode(getURL(0,1,1,1))?>" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Mua_hang')?></a></div>
                    <? }?>
                </td>
            </tr>
            <?
			}
			?>
        </table>
	<? } else {?>
    	<table class="product_table_thumbnail" cellpadding="0" cellspacing="0">
        <?
		$No = 0;
		while (($listing = mysql_fetch_array($db_listing->result)) || ($No % 3 != 0))
		{
			++$No;
			$detail_url = generate_detail_url('product', $listing['cat_id'], $listing['pro_id'], $listing['pro_name']);
		?>
        <? if ($No % 3 == 1) echo '<tr>';?>
            <td width="33%">
            <? if (isset($listing['pro_picture'])) {?>
            	<?
                //Get new dimensions
				if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/products_pictures/larger_' . $listing['pro_picture']))
				{
					list($width, $height) = getimagesize($_SERVER['DOCUMENT_ROOT'] . '/products_pictures/larger_' . $listing['pro_picture']);
				}
				else
				{
					$width = 0;
					$height = 0;
				}
				?>
				<div class="product_picture"><center><span><a class="product_picture_thumbnail" title="<?=$listing['pro_name']?>" href="<?=$detail_url?>" rel="<img src='/products_pictures/<?=$listing['pro_picture']?>' width='<?=$width?>' height='<?=$height?>' />" style="background-image:url(/products_pictures/small_<?=$listing['pro_picture']?>)"></a></span></center></div>
                <div class="product_name"><a href="<?=$detail_url?>"><?=$listing['pro_name']?></a></div><div class="product_price"><font lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Gia')?></font>: <span class="price"><?=($listing['pro_price'] > 0)?format_number($listing['pro_price']) . ' VNĐ':$con_message_zero_price?></span></div>
                <? if ($listing['pro_price'] > 0 && $listing['pro_quantity'] > 0) {?>
                <div class="add_to_cart"><center><a title="<?=tdt('Mua_hang')?>" href="<?=$con_root_path?>addtocart.php?iPro=<?=$listing['pro_id']?>&return=<?=base64_encode(getURL(0,1,1,1))?>" lang="<?=$array_lang[$lang_id][0]?>"><?=tdt('Mua_hang')?></a></center></div>
                <? }?>
                <!--<div class="product_compare"><input id="compare_check_<?=$No?>" name="compare_check[]" type="checkbox" value="<?=$listing['pro_id']?>" /><a href="#compare" onclick="document.getElementById('compare_check_<?=$No?>').checked=true;document.listudv_form.submit();" lang="<?=$array_lang[$lang_id][0]?>">So sánh</a></div>-->
			<? } // if (isset($listing['pro_picture'])) {?>
            </td>
        <? if ($No % 3 == 0) echo '</tr>';?>
        <?
		}
		?>
		</table>
    <? }?>
    </form>
    <div class="break_module"></div>
    <? if($total_record > $page_size){?>
    <div class="page_div"><?=generatePageBar($page_prefix, $current_page, $page_size, $total_record, $url, $normal_class, $selected_class, $previous, $next, $first, $last, $break_type)?></div>
    <? }?>
<? }?>
</div>
<div class="module_bottom"><div class="fl"></div><div class="fr"></div></div>